﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MoneyWentWhere.Utils
{
    public static class Lazy
    {
        /// <summary>
        /// Lazy initialization of a field using the anonymous method or the default constructor.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="field"></param>
        /// <returns></returns>
        public static T Get<T>(ref T field, Func<T> createFn = null)
            where T : class
        {
            if (field == null)
            {
                field = (createFn == null) ? 
                    Activator.CreateInstance<T>() : 
                    createFn(); 
            }
            return field; 
        }

    }
}
